home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-3.98 / gdb / valarith.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  18.1 KB  |  709 lines

  1. /* Perform arithmetic and other operations on values, for GDB.
  2.    Copyright (C) 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "param.h"
  22. #include "value.h"
  23. #include "expression.h"
  24. #include "target.h"
  25. #include <string.h>
  26.  
  27.  
  28. value value_x_binop ();
  29. value value_subscripted_rvalue ();
  30.  
  31. value
  32. value_add (arg1, arg2)
  33.     value arg1, arg2;
  34. {
  35.   register value val, valint, valptr;
  36.   register int len;
  37.  
  38.   COERCE_ARRAY (arg1);
  39.   COERCE_ARRAY (arg2);
  40.  
  41.   if ((TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
  42.        || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_PTR)
  43.       &&
  44.       (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT
  45.        || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT))
  46.     /* Exactly one argument is a pointer, and one is an integer.  */
  47.     {
  48.       if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
  49.     {
  50.       valptr = arg1;
  51.       valint = arg2;
  52.     }
  53.       else
  54.     {
  55.       valptr = arg2;
  56.       valint = arg1;
  57.     }
  58.       len = TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (valptr)));
  59.       if (len == 0) len = 1;    /* For (void *) */
  60.       val = value_from_long (builtin_type_long,
  61.                  value_as_long (valptr)
  62.                  + (len * value_as_long (valint)));
  63.       VALUE_TYPE (val) = VALUE_TYPE (valptr);
  64.       return val;
  65.     }
  66.  
  67.   return value_binop (arg1, arg2, BINOP_ADD);
  68. }
  69.  
  70. value
  71. value_sub (arg1, arg2)
  72.     value arg1, arg2;
  73. {
  74.   register value val;
  75.  
  76.   COERCE_ARRAY (arg1);
  77.   COERCE_ARRAY (arg2);
  78.  
  79.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
  80.     {
  81.       if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)
  82.     {
  83.       /* pointer - integer.  */
  84.       val = value_from_long
  85.         (builtin_type_long,
  86.          value_as_long (arg1)
  87.          - (TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)))
  88.         * value_as_long (arg2)));
  89.       VALUE_TYPE (val) = VALUE_TYPE (arg1);
  90.       return val;
  91.     }
  92.       else if (VALUE_TYPE (arg1) == VALUE_TYPE (arg2))
  93.     {
  94.       /* pointer to <type x> - pointer to <type x>.  */
  95.       val = value_from_long
  96.         (builtin_type_long,
  97.          (value_as_long (arg1) - value_as_long (arg2))
  98.          / TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))));
  99.       return val;
  100.     }
  101.       else
  102.     {
  103.       error ("\
  104. First argument of `-' is a pointer and second argument is neither\n\
  105. an integer nor a pointer of the same type.");
  106.     }
  107.     }
  108.  
  109.   return value_binop (arg1, arg2, BINOP_SUB);
  110. }
  111.  
  112. /* Return the value of ARRAY[IDX].  */
  113.  
  114. value
  115. value_subscript (array, idx)
  116.      value array, idx;
  117. {
  118.   if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_ARRAY
  119.       && VALUE_LVAL (array) != lval_memory)
  120.     return value_subscripted_rvalue (array, idx);
  121.   else
  122.     return value_ind (value_add (array, idx));
  123. }
  124.  
  125. /* Return the value of EXPR[IDX], expr an aggregate rvalue
  126.    (eg, a vector register).  This routine used to promote floats
  127.    to doubles, but no longer does.  */
  128.  
  129. value
  130. value_subscripted_rvalue (array, idx)
  131.      value array, idx;
  132. {
  133.   struct type *elt_type = TYPE_TARGET_TYPE (VALUE_TYPE (array));
  134.   int elt_size = TYPE_LENGTH (elt_type);
  135.   int elt_offs = elt_size * longest_to_int (value_as_long (idx));
  136.   value v;
  137.  
  138.   if (elt_offs >= TYPE_LENGTH (VALUE_TYPE (array)))
  139.     error ("no such vector element");
  140.  
  141.   v = allocate_value (elt_type);
  142.   bcopy (VALUE_CONTENTS (array) + elt_offs, VALUE_CONTENTS (v), elt_size);
  143.  
  144.   if (VALUE_LVAL (array) == lval_internalvar)
  145.     VALUE_LVAL (v) = lval_internalvar_component;
  146.   else
  147.     VALUE_LVAL (v) = not_lval;
  148.   VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
  149.   VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
  150.   VALUE_BITSIZE (v) = elt_size * 8;
  151.   return v;
  152. }
  153.  
  154. /* Check to see if either argument is a structure.  This is called so
  155.    we know whether to go ahead with the normal binop or look for a 
  156.    user defined function instead.
  157.  
  158.    For now, we do not overload the `=' operator.  */
  159.  
  160. int
  161. binop_user_defined_p (op, arg1, arg2)
  162.      enum exp_opcode op;
  163.      value arg1, arg2;
  164. {
  165.   if (op == BINOP_ASSIGN)
  166.     return 0;
  167.   return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
  168.       || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_STRUCT
  169.       || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
  170.           && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT)
  171.       || (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_REF
  172.           && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_STRUCT));
  173. }
  174.  
  175. /* Check to see if argument is a structure.  This is called so
  176.    we know whether to go ahead with the normal unop or look for a 
  177.    user defined function instead.
  178.  
  179.    For now, we do not overload the `&' operator.  */
  180.  
  181. int unop_user_defined_p (op, arg1)
  182.      enum exp_opcode op;
  183.      value arg1;
  184. {
  185.   if (op == UNOP_ADDR)
  186.     return 0;
  187.   return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
  188.       || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
  189.           && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT));
  190. }
  191.  
  192. /* We know either arg1 or arg2 is a structure, so try to find the right
  193.    user defined function.  Create an argument vector that calls 
  194.    arg1.operator @ (arg1,arg2) and return that value (where '@' is any
  195.    binary operator which is legal for GNU C++).
  196.  
  197.    OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
  198.    is the opcode saying how to modify it.  Otherwise, OTHEROP is
  199.    unused.  */
  200.  
  201. value
  202. value_x_binop (arg1, arg2, op, otherop)
  203.      value arg1, arg2;
  204.      enum exp_opcode op, otherop;
  205. {
  206.   value * argvec;
  207.   char *ptr;
  208.   char tstr[13];
  209.   int static_memfuncp;
  210.  
  211.   COERCE_ENUM (arg1);
  212.   COERCE_ENUM (arg2);
  213.  
  214.   /* now we know that what we have to do is construct our
  215.      arg vector and find the right function to call it with.  */
  216.  
  217.   if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
  218.     error ("Can't do that binary op on that type");  /* FIXME be explicit */
  219.  
  220.   argvec = (value *) alloca (sizeof (value) * 4);
  221.   argvec[1] = value_addr (arg1);
  222.   argvec[2] = arg2;
  223.   argvec[3] = 0;
  224.  
  225.   /* make the right function name up */  
  226.   strcpy(tstr, "operator__");
  227.   ptr = tstr+8;
  228.   switch (op)
  229.     {
  230.     case BINOP_ADD:    strcpy(ptr,"+"); break;
  231.     case BINOP_SUB:    strcpy(ptr,"-"); break;
  232.     case BINOP_MUL:    strcpy(ptr,"*"); break;
  233.     case BINOP_DIV:    strcpy(ptr,"/"); break;
  234.     case BINOP_REM:    strcpy(ptr,"%"); break;
  235.     case BINOP_LSH:    strcpy(ptr,"<<"); break;
  236.     case BINOP_RSH:    strcpy(ptr,">>"); break;
  237.     case BINOP_LOGAND:    strcpy(ptr,"&"); break;
  238.     case BINOP_LOGIOR:    strcpy(ptr,"|"); break;
  239.     case BINOP_LOGXOR:    strcpy(ptr,"^"); break;
  240.     case BINOP_AND:    strcpy(ptr,"&&"); break;
  241.     case BINOP_OR:    strcpy(ptr,"||"); break;
  242.     case BINOP_MIN:    strcpy(ptr,"<?"); break;
  243.     case BINOP_MAX:    strcpy(ptr,">?"); break;
  244.     case BINOP_ASSIGN:    strcpy(ptr,"="); break;
  245.     case BINOP_ASSIGN_MODIFY:    
  246.       switch (otherop)
  247.     {
  248.     case BINOP_ADD:      strcpy(ptr,"+="); break;
  249.     case BINOP_SUB:      strcpy(ptr,"-="); break;
  250.     case BINOP_MUL:      strcpy(ptr,"*="); break;
  251.     case BINOP_DIV:      strcpy(ptr,"/="); break;
  252.     case BINOP_REM:      strcpy(ptr,"%="); break;
  253.     case BINOP_LOGAND:   strcpy(ptr,"&="); break;
  254.     case BINOP_LOGIOR:   strcpy(ptr,"|="); break;
  255.     case BINOP_LOGXOR:   strcpy(ptr,"^="); break;
  256.     default:
  257.       error ("Invalid binary operation specified.");
  258.     }
  259.       break;
  260.     case BINOP_SUBSCRIPT: strcpy(ptr,"[]"); break;
  261.     case BINOP_EQUAL:      strcpy(ptr,"=="); break;
  262.     case BINOP_NOTEQUAL:  strcpy(ptr,"!="); break;
  263.     case BINOP_LESS:      strcpy(ptr,"<"); break;
  264.     case BINOP_GTR:       strcpy(ptr,">"); break;
  265.     case BINOP_GEQ:       strcpy(ptr,">="); break;
  266.     case BINOP_LEQ:       strcpy(ptr,"<="); break;
  267.     default:
  268.       error ("Invalid binary operation specified.");
  269.     }
  270.   argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
  271.   if (argvec[0])
  272.     {
  273.       if (static_memfuncp)
  274.     {
  275.       argvec[1] = argvec[0];
  276.       argvec++;
  277.     }
  278.       return target_call_function (argvec[0], 2 - static_memfuncp, argvec + 1);
  279.     }
  280.   error ("member function %s not found", tstr);
  281. #ifdef lint
  282.   return target_call_function (argvec[0], 2 - static_memfuncp, argvec + 1);
  283. #endif
  284. }
  285.  
  286. /* We know that arg1 is a structure, so try to find a unary user
  287.    defined operator that matches the operator in question.  
  288.    Create an argument vector that calls arg1.operator @ (arg1)
  289.    and return that value (where '@' is (almost) any unary operator which
  290.    is legal for GNU C++).  */
  291.  
  292. value
  293. value_x_unop (arg1, op)
  294.      value arg1;
  295.      enum exp_opcode op;
  296. {
  297.   value * argvec;
  298.   char *ptr;
  299.   char tstr[13];
  300.   int static_memfuncp;
  301.  
  302.   COERCE_ENUM (arg1);
  303.  
  304.   /* now we know that what we have to do is construct our
  305.      arg vector and find the right function to call it with.  */
  306.  
  307.   if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
  308.     error ("Can't do that unary op on that type");  /* FIXME be explicit */
  309.  
  310.   argvec = (value *) alloca (sizeof (value) * 3);
  311.   argvec[1] = value_addr (arg1);
  312.   argvec[2] = 0;
  313.  
  314.   /* make the right function name up */  
  315.   strcpy(tstr,"operator__");
  316.   ptr = tstr+8;
  317.   switch (op)
  318.     {
  319.     case UNOP_PREINCREMENT:    strcpy(ptr,"++"); break;
  320.     case UNOP_PREDECREMENT:    strcpy(ptr,"++"); break;
  321.     case UNOP_POSTINCREMENT:    strcpy(ptr,"++"); break;
  322.     case UNOP_POSTDECREMENT:    strcpy(ptr,"++"); break;
  323.     case UNOP_ZEROP:    strcpy(ptr,"!"); break;
  324.     case UNOP_LOGNOT:    strcpy(ptr,"~"); break;
  325.     case UNOP_NEG:    strcpy(ptr,"-"); break;
  326.     default:
  327.       error ("Invalid binary operation specified.");
  328.     }
  329.   argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
  330.   if (argvec[0])
  331.     {
  332.       if (static_memfuncp)
  333.     {
  334.       argvec[1] = argvec[0];
  335.       argvec++;
  336.     }
  337.       return target_call_function (argvec[0], 1 - static_memfuncp, argvec + 1);
  338.     }
  339.   error ("member function %s not found", tstr);
  340.   return 0;  /* For lint -- never reached */
  341. }
  342.  
  343. /* Perform a binary operation on two integers or two floats.
  344.    Does not support addition and subtraction on pointers;
  345.    use value_add or value_sub if you want to handle those possibilities.  */
  346.  
  347. value
  348. value_binop (arg1, arg2, op)
  349.      value arg1, arg2;
  350.      enum exp_opcode op;
  351. {
  352.   register value val;
  353.  
  354.   COERCE_ENUM (arg1);
  355.   COERCE_ENUM (arg2);
  356.  
  357.   if ((TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_FLT
  358.        &&
  359.        TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT)
  360.       ||
  361.       (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_FLT
  362.        &&
  363.        TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT))
  364.     error ("Argument to arithmetic operation not a number.");
  365.  
  366.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT
  367.       ||
  368.       TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_FLT)
  369.     {
  370.       double v1, v2, v;
  371.       v1 = value_as_double (arg1);
  372.       v2 = value_as_double (arg2);
  373.       switch (op)
  374.     {
  375.     case BINOP_ADD:
  376.       v = v1 + v2;
  377.       break;
  378.  
  379.     case BINOP_SUB:
  380.       v = v1 - v2;
  381.       break;
  382.  
  383.     case BINOP_MUL:
  384.       v = v1 * v2;
  385.       break;
  386.  
  387.     case BINOP_DIV:
  388.       v = v1 / v2;
  389.       break;
  390.  
  391.     default:
  392.       error ("Integer-only operation on floating point number.");
  393.     }
  394.  
  395.       val = allocate_value (builtin_type_double);
  396.       SWAP_TARGET_AND_HOST (&v, sizeof (v));
  397.       *(double *) VALUE_CONTENTS_RAW (val) = v;
  398.     }
  399.   else
  400.     /* Integral operations here.  */
  401.     {
  402.       /* Should we promote to unsigned longest?  */
  403.       if ((TYPE_UNSIGNED (VALUE_TYPE (arg1))
  404.        || TYPE_UNSIGNED (VALUE_TYPE (arg2)))
  405.       && (TYPE_LENGTH (VALUE_TYPE (arg1)) >= sizeof (unsigned LONGEST)
  406.           || TYPE_LENGTH (VALUE_TYPE (arg1)) >= sizeof (unsigned LONGEST)))
  407.     {
  408.       unsigned LONGEST v1, v2, v;
  409.       v1 = (unsigned LONGEST) value_as_long (arg1);
  410.       v2 = (unsigned LONGEST) value_as_long (arg2);
  411.       
  412.       switch (op)
  413.         {
  414.         case BINOP_ADD:
  415.           v = v1 + v2;
  416.           break;
  417.           
  418.         case BINOP_SUB:
  419.           v = v1 - v2;
  420.           break;
  421.           
  422.         case BINOP_MUL:
  423.           v = v1 * v2;
  424.           break;
  425.           
  426.         case BINOP_DIV:
  427.           v = v1 / v2;
  428.           break;
  429.           
  430.         case BINOP_REM:
  431.           v = v1 % v2;
  432.           break;
  433.           
  434.         case BINOP_LSH:
  435.           v = v1 << v2;
  436.           break;
  437.           
  438.         case BINOP_RSH:
  439.           v = v1 >> v2;
  440.           break;
  441.           
  442.         case BINOP_LOGAND:
  443.           v = v1 & v2;
  444.           break;
  445.           
  446.         case BINOP_LOGIOR:
  447.           v = v1 | v2;
  448.           break;
  449.           
  450.         case BINOP_LOGXOR:
  451.           v = v1 ^ v2;
  452.           break;
  453.           
  454.         case BINOP_AND:
  455.           v = v1 && v2;
  456.           break;
  457.           
  458.         case BINOP_OR:
  459.           v = v1 || v2;
  460.           break;
  461.           
  462.         case BINOP_MIN:
  463.           v = v1 < v2 ? v1 : v2;
  464.           break;
  465.           
  466.         case BINOP_MAX:
  467.           v = v1 > v2 ? v1 : v2;
  468.           break;
  469.           
  470.         default:
  471.           error ("Invalid binary operation on numbers.");
  472.         }
  473.  
  474.       val = allocate_value (BUILTIN_TYPE_UNSIGNED_LONGEST);
  475.       SWAP_TARGET_AND_HOST (&v, sizeof (v));
  476.       *(unsigned LONGEST *) VALUE_CONTENTS_RAW (val) = v;
  477.     }
  478.       else
  479.     {
  480.       LONGEST v1, v2, v;
  481.       v1 = value_as_long (arg1);
  482.       v2 = value_as_long (arg2);
  483.       
  484.       switch (op)
  485.         {
  486.         case BINOP_ADD:
  487.           v = v1 + v2;
  488.           break;
  489.           
  490.         case BINOP_SUB:
  491.           v = v1 - v2;
  492.           break;
  493.           
  494.         case BINOP_MUL:
  495.           v = v1 * v2;
  496.           break;
  497.           
  498.         case BINOP_DIV:
  499.           v = v1 / v2;
  500.           break;
  501.           
  502.         case BINOP_REM:
  503.           v = v1 % v2;
  504.           break;
  505.           
  506.         case BINOP_LSH:
  507.           v = v1 << v2;
  508.           break;
  509.           
  510.         case BINOP_RSH:
  511.           v = v1 >> v2;
  512.           break;
  513.           
  514.         case BINOP_LOGAND:
  515.           v = v1 & v2;
  516.           break;
  517.           
  518.         case BINOP_LOGIOR:
  519.           v = v1 | v2;
  520.           break;
  521.           
  522.         case BINOP_LOGXOR:
  523.           v = v1 ^ v2;
  524.           break;
  525.           
  526.         case BINOP_AND:
  527.           v = v1 && v2;
  528.           break;
  529.           
  530.         case BINOP_OR:
  531.           v = v1 || v2;
  532.           break;
  533.           
  534.         case BINOP_MIN:
  535.           v = v1 < v2 ? v1 : v2;
  536.           break;
  537.           
  538.         case BINOP_MAX:
  539.           v = v1 > v2 ? v1 : v2;
  540.           break;
  541.           
  542.         default:
  543.           error ("Invalid binary operation on numbers.");
  544.         }
  545.       
  546.       val = allocate_value (BUILTIN_TYPE_LONGEST);
  547.       SWAP_TARGET_AND_HOST (&v, sizeof (v));
  548.       *(LONGEST *) VALUE_CONTENTS_RAW (val) = v;
  549.     }
  550.     }
  551.  
  552.   return val;
  553. }
  554.  
  555. /* Simulate the C operator ! -- return 1 if ARG1 contains zeros.  */
  556.  
  557. int
  558. value_zerop (arg1)
  559.      value arg1;
  560. {
  561.   register int len;
  562.   register char *p;
  563.  
  564.   COERCE_ARRAY (arg1);
  565.  
  566.   len = TYPE_LENGTH (VALUE_TYPE (arg1));
  567.   p = VALUE_CONTENTS (arg1);
  568.  
  569.   while (--len >= 0)
  570.     {
  571.       if (*p++)
  572.     break;
  573.     }
  574.  
  575.   return len < 0;
  576. }
  577.  
  578. /* Simulate the C operator == by returning a 1
  579.    iff ARG1 and ARG2 have equal contents.  */
  580.  
  581. int
  582. value_equal (arg1, arg2)
  583.      register value arg1, arg2;
  584.  
  585. {
  586.   register int len;
  587.   register char *p1, *p2;
  588.   enum type_code code1;
  589.   enum type_code code2;
  590.  
  591.   COERCE_ARRAY (arg1);
  592.   COERCE_ARRAY (arg2);
  593.  
  594.   code1 = TYPE_CODE (VALUE_TYPE (arg1));
  595.   code2 = TYPE_CODE (VALUE_TYPE (arg2));
  596.  
  597.   if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
  598.     return value_as_long (arg1) == value_as_long (arg2);
  599.   else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
  600.        && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
  601.     return value_as_double (arg1) == value_as_double (arg2);
  602.  
  603.   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
  604.      is bigger.  */
  605.   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
  606.     return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2);
  607.   else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
  608.     return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2);
  609.  
  610.   else if (code1 == code2
  611.        && ((len = TYPE_LENGTH (VALUE_TYPE (arg1)))
  612.            == TYPE_LENGTH (VALUE_TYPE (arg2))))
  613.     {
  614.       p1 = VALUE_CONTENTS (arg1);
  615.       p2 = VALUE_CONTENTS (arg2);
  616.       while (--len >= 0)
  617.     {
  618.       if (*p1++ != *p2++)
  619.         break;
  620.     }
  621.       return len < 0;
  622.     }
  623.   else
  624.     {
  625.       error ("Invalid type combination in equality test.");
  626.       return 0;  /* For lint -- never reached */
  627.     }
  628. }
  629.  
  630. /* Simulate the C operator < by returning 1
  631.    iff ARG1's contents are less than ARG2's.  */
  632.  
  633. int
  634. value_less (arg1, arg2)
  635.      register value arg1, arg2;
  636. {
  637.   register enum type_code code1;
  638.   register enum type_code code2;
  639.  
  640.   COERCE_ARRAY (arg1);
  641.   COERCE_ARRAY (arg2);
  642.  
  643.   code1 = TYPE_CODE (VALUE_TYPE (arg1));
  644.   code2 = TYPE_CODE (VALUE_TYPE (arg2));
  645.  
  646.   if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
  647.     {
  648.       if (TYPE_UNSIGNED (VALUE_TYPE (arg1))
  649.        || TYPE_UNSIGNED (VALUE_TYPE (arg2)))
  650.     return ((unsigned LONGEST) value_as_long (arg1)
  651.         < (unsigned LONGEST) value_as_long (arg2));
  652.       else
  653.     return value_as_long (arg1) < value_as_long (arg2);
  654.     }
  655.   else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
  656.        && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
  657.     return value_as_double (arg1) < value_as_double (arg2);
  658.   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
  659.     return value_as_pointer (arg1) < value_as_pointer (arg2);
  660.  
  661.   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
  662.      is bigger.  */
  663.   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
  664.     return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2);
  665.   else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
  666.     return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2);
  667.  
  668.   else
  669.     {
  670.       error ("Invalid type combination in ordering comparison.");
  671.       return 0;
  672.     }
  673. }
  674.  
  675. /* The unary operators - and ~.  Both free the argument ARG1.  */
  676.  
  677. value
  678. value_neg (arg1)
  679.      register value arg1;
  680. {
  681.   register struct type *type;
  682.  
  683.   COERCE_ENUM (arg1);
  684.  
  685.   type = VALUE_TYPE (arg1);
  686.  
  687.   if (TYPE_CODE (type) == TYPE_CODE_FLT)
  688.     return value_from_double (type, - value_as_double (arg1));
  689.   else if (TYPE_CODE (type) == TYPE_CODE_INT)
  690.     return value_from_long (type, - value_as_long (arg1));
  691.   else {
  692.     error ("Argument to negate operation not a number.");
  693.     return 0;  /* For lint -- never reached */
  694.   }
  695. }
  696.  
  697. value
  698. value_lognot (arg1)
  699.      register value arg1;
  700. {
  701.   COERCE_ENUM (arg1);
  702.  
  703.   if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT)
  704.     error ("Argument to complement operation not an integer.");
  705.  
  706.   return value_from_long (VALUE_TYPE (arg1), ~ value_as_long (arg1));
  707. }
  708.  
  709.